package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author kaixuan
 * @since 2020-05-30
 */
@Api(description = "章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin //跨域访问
public class EduChapterController {

    @Autowired
    private EduChapterService chapterService;


    /**
     * 根据id查询数据
     * @param chapterId
     * @return
     */
    @ApiOperation("根据id查询数据")
    @GetMapping("getChapter/{chapterId}")
    public R getChapter(@ApiParam(value = "章节id",name = "chapterId",required = true)@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",eduChapter);
    }

    /**
     * 根据课程id获取课程里的章节、小节
     * @return
     */
    @ApiOperation("获取课程大纲tree列表")
    @GetMapping("chapterVideo/{courseId}")
    public R chapterVideo(@ApiParam(value = "课程id",name = "courseId",required = true) @PathVariable String courseId){
        List<ChapterVo> nodeList = chapterService.getChapterVideoNodeList(courseId);
        return R.ok().data("items",nodeList);
    }

    @ApiOperation("根据章节id逻辑删除章节")
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@ApiParam(value = "章节id",name = "chapterId",required = true)@PathVariable String chapterId){
        boolean flag = chapterService.removeChapter(chapterId);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }

    }

    @ApiOperation("添加章节信息")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter){
        boolean flag = chapterService.save(chapter);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("修改章节信息")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter){
        boolean flag = chapterService.updateById(chapter);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
}

